package vsm.vsm_server.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vsm.vsm_server.mapper.UserMapper;
import vsm.vsm_server.pojo.User.Login.Register;
import vsm.vsm_server.pojo.User.UserInfo;
import vsm.vsm_server.pojo.User.User;
import vsm.vsm_server.service.RelationService;
import vsm.vsm_server.service.UserService;
import vsm.vsm_server.service.VodService;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;
    //private final RelationController relationController; // 注入RelationController


    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;

    }
    @Autowired
    VodService vodService;

    @Autowired
    RelationService relationService;
    @Override
    public List<User> list() {
        return userMapper.list();
    }

    @Override
    public boolean delete(BigInteger id) {
       return userMapper.deleteById(id);
    }

    @Override
    public BigInteger create(Register register) {
        User newuser = new User();
        BeanUtils.copyProperties(register, newuser);
        newuser.setFans(0);
        newuser.setSubscriber(0);
        userMapper.create(newuser);
        return newuser.getUserId();
    }

    @Override
    public User getById(BigInteger userId) {
        return userMapper.getById(userId);
    }

    @Override
    public UserInfo getInfoById(BigInteger userId) {
        return userMapper.getInfoById(userId);
    }
    @Override
    public User getByName(String userName) throws Exception{
        User user = userMapper.getByName(userName);

        /*
        先判断有无该用户，再判断密码正确与否
         */
        if (Objects.isNull(user)){
            throw new RuntimeException("用户名或密码错误");
        }

        return userMapper.getByName(userName);
    }

    @Override
    public Boolean update(UserInfo newuser) {
        User user = new User();
        BeanUtils.copyProperties(newuser, user);
        return userMapper.update(user);
    }

    @Override
    public String getAvatarId(BigInteger userid) {
        return  userMapper.getAvatarId(userid);
    }

    @Override
    public List<UserInfo> userInfoListGetAvatar(List<UserInfo> userInfoList){
        List<String> avatarList = new ArrayList<>();
        if(!userInfoList.isEmpty()){
            for(UserInfo userInfo:userInfoList){
                String avatar = userInfo.getAvatar();
                if(avatar!=null){
                    avatarList.add(avatar);
                }
            }
            Map<String,String> avatarMap =  vodService.getPictureList(avatarList);
            for(UserInfo userInfo:userInfoList){
                String avatar = userInfo.getAvatar();
                if(avatar!=null) {
                    userInfo.setAvatar(avatarMap.get(avatar));
                }
            }
        }
        return userInfoList;
    }
    @Override
    public Map<BigInteger,UserInfo> getUserInfoList(List<BigInteger> userIdList) {
       List<UserInfo> userInfoList =   userMapper.getUserInfoList(userIdList);
       userInfoList = this.userInfoListGetAvatar(userInfoList);
       Map<BigInteger,UserInfo> userInfoMap = userInfoList.stream()
               .collect(Collectors.toMap(UserInfo::getUserId, userInfo->userInfo,(existingUserInfo,newUserInfo)->newUserInfo));
        return userInfoMap;
    }

    /*
    校验粉丝数量
     */
    @Override
    public void verifyFans(BigInteger userId) {
        //校验粉丝和关注的数量
        List<BigInteger> fansl = relationService.getFans(userId);
        List<BigInteger> subscribesl = relationService.getSubs(userId);
        Integer fans = (fansl != null) ? fansl.size() : 0;
        Integer subscribes = (subscribesl != null) ? subscribesl.size() : 0;
        userMapper.verifyFans(userId, fans, subscribes);
    }

    @Override
    public void addFans(BigInteger userId) {
        userMapper.addFans(userId);
    }

    @Override
    public void deFans(BigInteger userId) {
        userMapper.deFans(userId);
    }

    @Override
    public void addSubs(BigInteger userId) {
        userMapper.addSubs(userId);
    }

    @Override
    public void deSubs(BigInteger userId) {
        userMapper.deSubs(userId);
    }
}
